package com.masiyi.easyexcel.service;

import com.masiyi.easyexcel.doman.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author masiyi
 * @Date 2023/3/29 10:38
 * @PackageName:com.masiyi.easydatabaseexport.server
 * @ClassName: DataService
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class DataService {


    @Autowired
    private JdbcTemplate jdbcTemplate;


    public List<String> getAllTableNames() {
        return jdbcTemplate.queryForList("SHOW TABLES", String.class);
    }


    public List<List<Table>> initData() {
        List<String> allTableNames = getAllTableNames();
        ArrayList<List<Table>> lists = new ArrayList<>();
        for (String tableName : allTableNames) {
            List<Map<String, Object>> tables = jdbcTemplate.queryForList("show full COLUMNS FROM " + tableName);
            ArrayList<Table> tables1 = new ArrayList<>();
            for (Map<String, Object> table : tables) {
                Object field = table.get("Field");
                Object type = table.get("Type");
                Object comment = table.get("Comment");
                Table ta = new Table(tableName,(String) field, (String) type, (String) comment);
                tables1.add(ta);
            }
            lists.add(tables1);
        }
        return lists;
    }
}
